package com.zmxstudy.rbac.controller;

import com.zmxstudy.rbac.base.BaseController;
import com.zmxstudy.rbac.entity.Auth;
import com.zmxstudy.rbac.entity.RoleAuth;
import com.zmxstudy.rbac.entity.RoleAuths;
import com.zmxstudy.rbac.entity.RolePermission;
import com.zmxstudy.rbac.service.AuthService;
import jakarta.validation.constraints.NotEmpty;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author star
 */
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController<AuthService, Auth> {
    /**
     * 获取但前用户的菜单
     * @param username
     * @return
     */
     @GetMapping("/menu")
    public List<Auth> menu(@RequestAttribute("username") String username){
//         System.out.println(username);
         //用户名可以从token中取出
         return baseService.getMenu(username);
     }

     @GetMapping("/info")
    public List<RoleAuth> findAllRoleAuths(){
         return baseService.AllRoleAuths();
     }


     @DeleteMapping("/delRoleAuth")
    public boolean delRoleAuth(@RequestHeader("Role-Id") @NotEmpty int roleId, @RequestHeader("Auth-Id") @NotEmpty int authId){
         if(baseService.delRoleAuth(roleId,authId)){
             return true;
         }
         return false;
     }


    @PostMapping("/plusRoleAuth")
    public boolean plusRoleAuth(@RequestBody RoleAuths RoleAuths){
         int roleId = RoleAuths.getRoleId();
         int authId = RoleAuths.getAuthId();
        if(baseService.plusRoleAuth(roleId,authId)){
            return true;
        }
        return false;
    }


}
